//
//  HomeShopcartBtnView.m
//  FD
//
//  Created by xiaofang wang on 2018/11/27.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//
//  首页购物车自定义按钮view
// size(70,70)
#import "HomeShopcartBtnView.h"
@interface HomeShopcartBtnView()
/**1.图片*/
@property (nonatomic, strong) UIImageView *iv;
/**2.角标*/
@property (nonatomic, strong) UILabel *jiaobiaoLb;
@end

@implementation HomeShopcartBtnView
#pragma mark - 一 init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapShopcartBtnViewAction)];
        [self addGestureRecognizer:tap];
    }
    return self;
}
- (void)setupView {
    [self addSubview:self.iv];
    [self addSubview:self.jiaobiaoLb];
    
    self.jiaobiaoLb.hidden = YES; //默认隐藏,请求到数据之后在显示
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.iv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    //显示购物车数量 
    [self.jiaobiaoLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(15, 15));
        make.top.equalTo(self.mas_top).offset(15);
        make.right.equalTo(self.mas_right).offset(-20);
    }];
    
}

#pragma mark - 二 getter
//购物车
- (UIImageView *)iv {
    if (!_iv) {
        _iv = [UIImageView new];
        _iv.image = [UIImage imageNamed:@"购物车"];
    }
    return _iv;
}
//(15,15)
- (UILabel *)jiaobiaoLb {
    if (!_jiaobiaoLb) {
        _jiaobiaoLb = [UILabel new];
        _jiaobiaoLb.backgroundColor = [UIColor rb_colorHex:0xf2661b];
        _jiaobiaoLb.textColor = [UIColor whiteColor];
        _jiaobiaoLb.textAlignment = NSTextAlignmentCenter;
        _jiaobiaoLb.font = [UIFont systemFontOfSize:10.0];
        //切圆角
        _jiaobiaoLb.layer.masksToBounds = YES;
        _jiaobiaoLb.layer.cornerRadius = 7.0;
    }
    return _jiaobiaoLb;
}

#pragma mark - 三 点击事件
- (void)tapShopcartBtnViewAction {
    RBFunctionLog;
    if (self.clickShopcartBtnViewBlock) {
        self.clickShopcartBtnViewBlock();
    }
}

/**更新购物车商品数量*/
- (void)updateShopcartBtnViewCount:(NSInteger)count {
    self.jiaobiaoLb.text = [NSString stringWithFormat:@"%ld",count];
    
    if (count) { //有数据就显示
        self.jiaobiaoLb.hidden = NO;
    } else { //没数据就隐藏
        self.jiaobiaoLb.hidden = YES; //隐藏
    }
}

@end
